package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 门户页面中商品检索页面渲染
     *
     * @param searchParam
     * @param model
     * @return
     */
    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model) {
        Result<Map> result = listFeignClient.list(searchParam);
        String urlParam = this.makeUrlParma(searchParam);
        String trademarkParam = this.makeTradeMarkParam(searchParam.getTrademark());
        List<SearchAttr> propsParamList = this.makePropsList(searchParam.getProps());
        Map orderMap = this.makeOrderMap(searchParam.getOrder());

        model.addAttribute("urlParam", urlParam);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("orderMap",orderMap);
        model.addAttribute("searchParam", searchParam);
        model.addAllAttributes(result.getData());

        System.out.println("urlParam = " + urlParam);
        return "list/index";
    }

    private Map makeOrderMap(String order) {
        Map map = new HashMap();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
            }

        } else {
            //  默认排序规则
            map.put("type", "1");
            map.put("sort", "desc");
        }
        return map;
    }

    private List<SearchAttr> makePropsList(String[] props) {
        List<SearchAttr> searchAttrList = new ArrayList<>();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    SearchAttr searchAttr = new SearchAttr();
                    searchAttr.setAttrId(Long.parseLong(split[0]));
                    searchAttr.setAttrName(split[2]);
                    searchAttr.setAttrValue(split[1]);
                    searchAttrList.add(searchAttr);
                }
            }
        }
        return searchAttrList;
    }

    private String makeTradeMarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            return "品牌:" + split[1];
        }
        return "";
    }

    private String makeUrlParma(SearchParam searchParam) {
        StringBuffer url = new StringBuffer();
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())) {
            url.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())) {
            url.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())) {
            url.append("category3Id=").append(searchParam.getCategory3Id());
        }
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            url.append("keyword=").append(searchParam.getKeyword());
        }
        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            if (url.length() > 0) {
                url.append("&trademark=").append(searchParam.getTrademark());
            }
        }
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            //  循环遍历
            for (String prop : props) {
                if (url.length() > 0) {
                    url.append("&props=").append(prop);
                }
            }
        }
        return "list.html?" + url.toString();
    }
}
